package cn.lf.android.lawfirm.distribution;

import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.controller.DistributionInfoConroller;
import cn.lf.android.lawfirm.distribution.controller.DistributionMemberConroller;
import cn.lf.android.lawfirm.distribution.modle.DistributionInfo;
import cn.lf.android.lawfirm.distribution.modle.TrialLevel;
import cn.lf.android.lawfirm.distribution.controller.TrialLevelConroller;
import cn.lf.android.lawfirm.distribution.view.PeopleAdapter;
import cn.lf.android.lawfirm.inputsource.TheSourcePeopleListActivity;
import cn.lf.android.lawfirm.inputsource.conntroller.AcceptingDepartmentController;
import cn.lf.android.lawfirm.inputsource.model.AcceptingDepartment;
import cn.lf.android.lawfirm.inputsource.model.People;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.timepicker.TimePicker;
import cn.lf.android.lawfirm.view.NoScrollListView;

import static cn.lf.android.lawfirm.R.id.eb_tv_right;

/**
 * 案源分配页
 */
public class DistributionActivity extends BaseActivity implements AcceptingDepartmentController.AcceptingDepartmentCallback, TrialLevelConroller.TrialLevelCallback, DistributionMemberConroller.DistributionMemberCallback, DistributionInfoConroller.DistributionInfoCallback, AcceptingDepartmentController.AcceptingDepartmentChangeCallback {

    /**
     * 返回
     **/
    private ImageButton img_left;
    /**
     * 标题
     **/
    private TextView mtitle, eb_tv_right;
    private String caseID = "";
    private NoScrollListView assistant_lawyer_list, two_lawyer_list, main_lawyer_list;
    private TextView projectofficer, department, riskofficer, content_people, triallevel;
    private AutoRelativeLayout rl_people, rl_risk_officer, rl_caseAcceptDepartment, rl_project_officer, rl_trial_level;
    private ImageView img_add_assistant, img_add_two, img_add_main;

    //受理部门
    private List<AcceptingDepartment> departmentList = new ArrayList<AcceptingDepartment>();
    private AcceptingDepartmentController mAcceptingDepartmentController;
    private TimePicker mTimePicker;
    //案件审级
    private TimePicker mTimePicker_triallevel;
    private List<TrialLevel> mList_triallevel = new ArrayList<TrialLevel>();

    private TrialLevelConroller mTrialLevelConroller;
    private DistributionMemberConroller mDistributionMemberConroller;
    private DistributionInfoConroller mDistributionInfoConroller;
    //--参数--
    /**
     * 受理部门编号
     **/
    private String department_num;
    /**
     * 参与审级名称，默认为全部（非必填）
     **/
    private String levelName = "全部";
    //安源人
    private People sourse_people;
    //风险责任人
    private People riskofficer_people;
    //项目负责人
    private People projectofficer_people;
    //主办律师
    List<People> mList_main_lwayer = new ArrayList<People>();
    private PeopleAdapter mAdapter_main_lwayer;
    //协办律师
    List<People> mList_two_lwayer = new ArrayList<People>();
    private PeopleAdapter mAdapter_two_lwayer;
    //助理
    List<People> mList_assistant_lwayer = new ArrayList<People>();
    private PeopleAdapter mAdapter_assistant_lwayer;

    @Override
    public Object getContentResView() {
        return R.layout.activity_distribution;
    }

    @Override
    public void initView() {
        initTitle();
        caseID = getIntent().getStringExtra("caseID");
        assistant_lawyer_list = (NoScrollListView) findViewById(R.id.assistant_lawyer_list);
        two_lawyer_list = (NoScrollListView) findViewById(R.id.two_lawyer_list);
        main_lawyer_list = (NoScrollListView) findViewById(R.id.main_lawyer_list);
        projectofficer = (TextView) findViewById(R.id.projectofficer);
        department = (TextView) findViewById(R.id.department);
        riskofficer = (TextView) findViewById(R.id.riskofficer);
        content_people = (TextView) findViewById(R.id.content_people);
        triallevel = (TextView) findViewById(R.id.content_people);
        triallevel = (TextView) findViewById(R.id.triallevel);
        rl_trial_level = (AutoRelativeLayout) findViewById(R.id.rl_trial_level);
        rl_trial_level.setOnClickListener(this);
        rl_people = (AutoRelativeLayout) findViewById(R.id.rl_people);
        rl_people.setOnClickListener(this);
        rl_risk_officer = (AutoRelativeLayout) findViewById(R.id.rl_risk_officer);
        rl_risk_officer.setOnClickListener(this);
        rl_caseAcceptDepartment = (AutoRelativeLayout) findViewById(R.id.rl_caseAcceptDepartment);
        rl_caseAcceptDepartment.setOnClickListener(this);
        rl_project_officer = (AutoRelativeLayout) findViewById(R.id.rl_project_officer);
        rl_project_officer.setOnClickListener(this);
        img_add_assistant = (ImageView) findViewById(R.id.img_add_assistant);
        img_add_assistant.setOnClickListener(this);
        img_add_two = (ImageView) findViewById(R.id.img_add_two);
        img_add_two.setOnClickListener(this);
        img_add_main = (ImageView) findViewById(R.id.img_add_main);
        img_add_main.setOnClickListener(this);

        mAdapter_main_lwayer = new PeopleAdapter(this, mList_main_lwayer, "主办律师");
        mAdapter_two_lwayer = new PeopleAdapter(this, mList_two_lwayer, "协办律师");
        mAdapter_assistant_lwayer = new PeopleAdapter(this, mList_assistant_lwayer, "助理");

        assistant_lawyer_list.setAdapter(mAdapter_assistant_lwayer);
        main_lawyer_list.setAdapter(mAdapter_main_lwayer);
        two_lawyer_list.setAdapter(mAdapter_two_lwayer);

    }


    private void initTitle() {
        img_left = (ImageButton) findViewById(R.id.img_left);
        img_left.setVisibility(View.VISIBLE);
        img_left.setOnClickListener(this);
        mtitle = (TextView) findViewById(R.id.eb_tv_title);
        mtitle.setVisibility(View.VISIBLE);
        mtitle.setText("案源分派");
        eb_tv_right = (TextView) findViewById(R.id.eb_tv_right);
        eb_tv_right.setText("确认");
        eb_tv_right.setOnClickListener(this);
    }

    @Override
    public void initData() {
        //滑动退出
//        addSwipeFinishLayout();
        mAcceptingDepartmentController = new AcceptingDepartmentController(this, this);
        mTrialLevelConroller = new TrialLevelConroller(this, this);
        mDistributionMemberConroller = new DistributionMemberConroller(this, this);
        mDistributionInfoConroller = new DistributionInfoConroller(this, this);
        mDistributionInfoConroller.onDistributInfo(caseID);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.eb_tv_right:
                finish();
                break;
            case R.id.img_left:
                finish();
                break;
            case R.id.rl_trial_level://案件所属审级
                mTimePicker_triallevel.show(getWindow().getDecorView());
                break;
            case R.id.rl_people://安源人
                Intent intent = new Intent(this, TheSourcePeopleListActivity.class);
                intent.putExtra("title", "案源人");
                intent.putExtra("isBusiness", "false");
                startActivityForResult(intent, 1004);
                break;
            case R.id.rl_risk_officer://风险责任人
                Intent intent2 = new Intent(this, TheSourcePeopleListActivity.class);
                intent2.putExtra("title", "风险责任人");
                intent2.putExtra("isBusiness", "true");
                startActivityForResult(intent2, 1002);
                break;
            case R.id.rl_caseAcceptDepartment://受理部门
                mTimePicker.show(getWindow().getDecorView());
                break;
            case R.id.rl_project_officer://项目负责人
                Intent intent3 = new Intent(this, TheSourcePeopleListActivity.class);
                intent3.putExtra("title", "项目负责人");
                intent3.putExtra("isBusiness", "true");
                startActivityForResult(intent3, 1003);
                break;
            case R.id.img_add_assistant://加助理
                Intent intent5 = new Intent(this, TheSourcePeopleListActivity.class);
                intent5.putExtra("title", "助理");
                intent5.putExtra("isBusiness", "false");
                startActivityForResult(intent5, 1005);
                break;
            case R.id.img_add_two://加协办律师
                Intent intent6 = new Intent(this, TheSourcePeopleListActivity.class);
                intent6.putExtra("title", "协办律师");
                intent6.putExtra("isBusiness", "true");
                startActivityForResult(intent6, 1006);
                break;
            case R.id.img_add_main://加主板律师
                Intent intent7 = new Intent(this, TheSourcePeopleListActivity.class);
                intent7.putExtra("title", "主办律师");
                intent7.putExtra("isBusiness", "true");
                startActivityForResult(intent7, 1007);
                break;

            default:
                break;
        }
    }

    public void addOrReducePeople(String memberId, String type, String roleName, String userName, int position) {
        mDistributionMemberConroller.onDistributionMember(caseID, memberId, type, roleName, levelName, userName, position);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1004://安源人
                    sourse_people = (People) data.getSerializableExtra("people");
                    DebugLog.e("安源人6==" + sourse_people);
                    if (content_people.getText().toString().length() > 0) {
                        addOrReducePeople(sourse_people.userID, "change", "案源人", sourse_people.userName, 0);
                    } else {
                        addOrReducePeople(sourse_people.userID, "join", "案源人", sourse_people.userName, 0);
                    }
                    break;
                case 1007://主办律师
                    People p = (People) data.getSerializableExtra("people");
                    mList_main_lwayer.add(p);
                    addOrReducePeople(p.userID, "join", "主办律师", p.userName, mList_main_lwayer.size());
                    break;
                case 1006://协办律师
                    People p1 = (People) data.getSerializableExtra("people");
                    mList_two_lwayer.add(p1);
                    addOrReducePeople(p1.userID, "join", "协办律师", p1.userName, mList_two_lwayer.size());
                    break;
                case 1005://助理
                    People p2 = (People) data.getSerializableExtra("people");
                    mList_assistant_lwayer.add(p2);
                    addOrReducePeople(p2.userID, "join", "助理", p2.userName, mList_assistant_lwayer.size());
                    break;
                case 1003://项目负责人
                    projectofficer_people = (People) data.getSerializableExtra("people");
                    if (projectofficer.getText().toString().length() > 0) {
                        addOrReducePeople(projectofficer_people.userID, "change", "项目负责人", projectofficer_people.userName, 0);
                    } else {
                        addOrReducePeople(projectofficer_people.userID, "join", "项目负责人", projectofficer_people.userName, 0);
                    }
                    break;
                case 1002://风险责任人
                    riskofficer_people = (People) data.getSerializableExtra("people");
                    if (riskofficer.getText().toString().length() > 0) {
                        addOrReducePeople(riskofficer_people.userID, "change", "风险责任人", riskofficer_people.userName, 0);
                    } else {
                        addOrReducePeople(riskofficer_people.userID, "join", "风险责任人", riskofficer_people.userName, 0);
                    }
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    public void onSuccessed(List<AcceptingDepartment> beans) {
        departmentList.clear();
        departmentList.addAll(beans);
        List<String> list_strs = new ArrayList<String>();
        for (AcceptingDepartment department : departmentList) {
//            if ("true".equals(department.isChecked)) {
            list_strs.add(department.lsysDeptName);
//            }
        }
        mTimePicker = new TimePicker(this, 1, list_strs,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        department.setText(timeString);
                        for (AcceptingDepartment bean : departmentList) {
                            if (timeString.equals(bean.lsysDeptName)) {
                                department_num = bean.lsysDeptID;
                            }
                        }
                        mAcceptingDepartmentController.onAcceptingDepartmentChange(caseID, mDistributionInfo.levelID, department_num);
                    }
                });
        DebugLog.e("编号==" + department_num);
        mTrialLevelConroller.onTrialLevel(caseID, "");
    }

    @Override
    public void onFailed(String errorMsg) {

    }

    @Override
    public void onTrialLevelSuccessed(List<TrialLevel> beans) {
        mList_triallevel.addAll(beans);
        List<String> list_strs = new ArrayList<String>();
        for (TrialLevel trialLevel : mList_triallevel) {
            list_strs.add(trialLevel.levelName);
        }
        mTimePicker_triallevel = new TimePicker(this, 1, list_strs,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        triallevel.setText(timeString);
                        levelName = timeString;
                    }
                });
    }

    @Override
    public void onTrialLevelFailed() {

    }

    @Override
    public void onDistributionMemberSuccessed(String type, String roleName, int position) {
        if ("案源人".equals(roleName)) {
            content_people.setText(sourse_people.userName);
        } else if ("项目负责人".equals(roleName)) {
            projectofficer.setText(projectofficer_people.userName);
        } else if ("风险责任人".equals(roleName)) {
            riskofficer.setText(riskofficer_people.userName);
        } else if ("主办律师".equals(roleName)) {
            if ("quit".equals(type)) {
                mList_main_lwayer.remove(position);
            }
            mAdapter_main_lwayer.notifyDataSetChanged();
        } else if ("协办律师".equals(roleName)) {
            if ("quit".equals(type)) {
                mList_two_lwayer.remove(position);
            }
            mAdapter_two_lwayer.notifyDataSetChanged();
        } else if ("助理".equals(roleName)) {
            if ("quit".equals(type)) {
                mList_assistant_lwayer.remove(position);
            }
            mAdapter_assistant_lwayer.notifyDataSetChanged();
        }
        DistributionListActivity.isRefresh = true;
    }

    @Override
    public void onDistributionMemberFailed(String type, String roleName, int position) {
        if ("主办律师".equals(roleName)) {
            if ("join".equals(type)) {
                mList_main_lwayer.remove(position);
            }
            mAdapter_main_lwayer.notifyDataSetChanged();
        } else if ("协办律师".equals(roleName)) {
            if ("join".equals(type)) {
                mList_two_lwayer.remove(position);
            }
            mAdapter_two_lwayer.notifyDataSetChanged();
        } else if ("助理".equals(roleName)) {
            if ("join".equals(type)) {
                mList_assistant_lwayer.remove(position);
            }
            mAdapter_assistant_lwayer.notifyDataSetChanged();
        }
    }

    DistributionInfo mDistributionInfo;

    @Override
    public void onDistributionInfoSuccessed(DistributionInfo beans) {
        DebugLog.e("beans====" + beans);
        mDistributionInfo = beans;
        sourse_people = null;
        riskofficer_people = null;
        projectofficer_people = null;
        mList_main_lwayer.clear();
        mList_two_lwayer.clear();
        mList_assistant_lwayer.clear();
        triallevel.setText(beans.levelName);
        String dd = "";
        for (int i = 0; i < beans.department.size(); i++) {
            if (beans.department.get(i).isChecked) {
                if (dd.length() == 0) {
                    dd = beans.department.get(i).lsysDeptName;
                } else {
                    dd = dd + "," + beans.department.get(i).lsysDeptName;
                }
            }
        }
        department.setText(dd);
        for (People people : beans.members) {
            people.userID = people.userId;
            if ("案源人".equals(people.MemberRole)) {
                sourse_people = people;
                content_people.setText(people.userName);
            }
            if ("风险责任人".equals(people.MemberRole)) {
                riskofficer_people = people;
                riskofficer.setText(people.userName);
            }
            if ("项目负责人".equals(people.MemberRole)) {
                projectofficer_people = people;
                projectofficer.setText(people.userName);
            }
            if ("主办律师".equals(people.MemberRole)) {
                mList_main_lwayer.add(people);
            }
            if ("协办律师".equals(people.MemberRole)) {
                mList_two_lwayer.add(people);
            }
            if ("助理".equals(people.MemberRole)) {
                mList_assistant_lwayer.add(people);
            }
        }
        mAdapter_main_lwayer.notifyDataSetChanged();
        mAdapter_two_lwayer.notifyDataSetChanged();
        mAdapter_assistant_lwayer.notifyDataSetChanged();
        mAcceptingDepartmentController.onCaseAcceptingDepartment(caseID, beans.levelID);
    }

    @Override
    public void onDistributionInfoFailed() {

    }

    @Override
    public void onChangeSuccessed(String beans) {
        mDistributionInfoConroller.onDistributInfo(caseID);
    }

    @Override
    public void onChangeFailed(String errorMsg) {

    }
}